package com.ysq.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysq.controller.result.Result;
import com.ysq.entity.AddressBook;
import com.ysq.entity.Category;
import com.ysq.entity.Employee;
import com.ysq.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: CategoryController
 * Package: com.ysq.controller
 * Description:
 *
 * @Author YSQ
 * @Create 2023/1/3 - 11:00
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService service;

    /**
     * 新增分类
     * @param category 接收前端传来的表单数据
     * @return 返回统一结果封装类
     */
    @PostMapping
    public Result<String> save(@RequestBody Category category) {
        boolean flag = service.save(category);
        if (flag) {
            return Result.success("新增分类成功");
        }
        return Result.error("新增分类失败");
    }

    /**
     * 分类管理分页查询
     * @param page 第几页数据
     * @param pageSize 一页几条数据
     * @return 返回统一封装类, data属性封装page
     */
    @GetMapping("/page")
    public Result<Page<Category>> page(int page, int pageSize) {
        Page<Category> categoryPage = service.selectPage(page, pageSize);
        return Result.success(categoryPage);
    }

    /**
     * 分类删除
     * @param id 接收前端发送的分类ID
     * @return 返回统一结果封装类
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam("ids") Long id) {
        boolean flag = service.delete(id);
        if (flag) {
            return Result.success("分类信息删除成功~~~");
        }
        return Result.error("分类信息删除失败~~~");
    }

    /**
     * 分类修改
     * @param category 接收前端传来的category对象, 里面包含id, name, sort数据
     * @return 返回统一结果封装类
     */
    @PutMapping
    public Result<String> update(@RequestBody Category category) {
        boolean flag = service.update(category);
        if (flag) {
            return Result.success("修改分类信息成功~~~");
        }
        return Result.error("修改分类信息失败~~~");
    }

    /**
     * 新增菜品页面的菜品分类下拉框
     * @param category 前端发送Category的type属性, 包装成category对象进行条件查询
     * @return 返回统一结果封装类
     */
    @GetMapping("/list")
    public Result<List<Category>> DishCategoryList(Category category) {
        List<Category> categories = service.selectByType(category);
        return Result.success(categories);
    }
}
